<?php

/**
 * album actions.
 *
 * @package    iEasy
 * @subpackage album
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class albumActions extends adminBase
{
 /**
  * Executes index action
  *
  * @param sfRequest $request A request object
  */
  public function executeIndex(sfWebRequest $request)
  {    $navigator = "/backend.php/album/index?";
        $this->navigator = "";
        $this->catalogId = $request->getParameter("catalogId");
        $this->subCatalogId = $request->getParameter("subCatalogId");
        $this->contentTypeId = $request->getParameter("contentTypeId");
        $submitType = $request->getParameter("submitType");
      if($submitType != null)
      {
          $content=$request->getParameter("content");
          if(!empty($content))
          {
              $Ids=explode(" ",$content);
              for($i=0;$i<count($Ids);$i++)
              {
                  if(!empty($Ids[$i]))
                  {
                      $currentPhoto=@Doctrine_Core::getTable('PhotoGroup')->find($Ids[$i]);
                      if(!empty($currentPhoto))
                      {
                          $currentPhoto->setModifyTime(date("Y-m-d H:i:s"));
                          switch($submitType)
                              {
                                  case "delete": //
                                      $currentPhoto->setStatusId(1006);
                                      $this->modifyPhotoFocus($currentPhoto,"delete");
                                      break;
                                  case "publish":
                                      if($currentPhoto->getStatusId() != 1005)
                                      {
                                         $currentPhoto->setPublishTime(date("Y-m-d H:i:s"));
                                         $currentPhoto->setStatusId(1005); //
                                      }
                                      $this->modifyPhotoFocus($currentPhoto,"publish");
                                      break;
                                  case "cancelPublish":
                                      $currentPhoto->setModifyTime(date("Y-m-d H:i:s"));
                                      $currentPhoto->setStatusId(1004); //不审核
                                      $this->modifyPhotoFocus($currentPhoto,"cancelPublish");
                                      break;
                                  case "recommend":
                                      $this->modifyPhotoRecommend($currentPhoto,"add");
                                      $currentPhoto->setIsRecommendB("1");
                                      break;
                                  case "cancelRecommend":
                                      $this->modifyPhotoRecommend($currentPhoto,"delete");
                                      $currentPhoto->setIsRecommendB("0");
                                      break;
                                  
                                  case "top":
                                      $currentPhoto->setIsOnTopB("1");
                                      $this->modifyPhotoRecommend($currentPhoto,"top");
                                      $this->modifyPhotoFocus($currentPhoto,"top");
                                      break;
                                  case "cancelTop":
                                     $currentPhoto->setIsOnTopB("0");
                                     $this->modifyPhotoRecommend($currentPhoto,"cancelTop");
                                      $this->modifyPhotoFocus($currentPhoto,"cancelTop");
                                      break;
                                  case "cancelFocus":
                                      $currentPhoto->setIsFocusB("0");
                                      $this->modifyPhotoFocus($currentPhoto,"delete");
                                      break;
                              }
                              $currentPhoto->save();
                      }
                  }
              }
          }
      }

      $this->recordCount = $request->getParameter('recordCount');
        $this->currentPage = $request->getParameter('currentPage');
        if (null == $this->recordCount || "" == $this->recordCount)
            $this->recordCount = 20;
        if (null == $this->currentPage || "" == $this->currentPage)
            $this->currentPage = 1;

        $query = Doctrine_Query::create()->from("PhotoGroup");
        $catalog = null;
        if ($this->catalogId > 0) {
            $query = $query->where('catalog_id=' . $this->catalogId);
            $catalog = Doctrine_Core::getTable("Catalog")->find($this->catalogId);
            if ($catalog != null)
                $this->navigator = "<a href='" . $navigator . "catalogId=" . $this->catalogId . "'>" . $catalog->getDescription() . "</a>";
        }
        $contentType = Doctrine_Core::getTable("ContentType")->find($this->contentTypeId);
        if ($contentType != null) {
            if ($this->navigator != "")
                $this->navigator = $this->navigator . " -> ";
            $this->navigator = $this->navigator . "<a href='" . $navigator . "contentTypeId=" . $this->contentTypeId;
            if ($catalog != null)
                $this->navigator = $this->navigator . "&catalogId=" . $catalog->getCatalogId();
            $this->navigator = $this->navigator . "'>" . $contentType->getDescription() . "</a>";
        }

        $this->searchProperty = $request->getParameter("searchProperty");
      $searchDelete = "false";
      switch($this->searchProperty)
      {
          case "top":
              $query = $query->andWhere("is_on_top_b=1");
              break;
          case "recommend":
              $query = $query->andWhere("is_recommend_b=1");
              break;
          case "publish":
              $query = $query->andWhere("status_id=1005");
              break;
          case "delete":
              $searchDelete = "1";
              $query = $query->andWhere("status_id=1006");
              break;
          case "focus":
              $query = $query->andWhere("is_focus_b=1");
              break;
      }
      $this->searchField = $request->getParameter("searchField");
      switch($this->searchField)
      {
          case "title":
              $query = $query->andWhere("title like '%".trim($request->getParameter("searchText"))."%'");
              break;
          case "keyword":
              $query = $query->andWhere("keyword like '%".trim($request->getParameter("searchText"))."%'");
              break;
          case "tag":
              $query = $query->andWhere("tag like '%".trim($request->getParameter("searchText"))."%'");
              break;
      }
      $this->searchText = trim($request->getParameter("searchText"));
      if($this->searchField != null)
          $this->searchStr = $this->searchStr."searchField=".$this->searchField;
      else
      {
          if($submitType != null && $submitType == "search") //默认搜索人名
              $query = $query->andWhere("title like '%".trim($request->getParameter("searchText"))."%'");
      }
      if($this->searchProperty != null)
      {
          if($this->searchStr != "")
              $this->searchStr = $this->searchStr."&searchProperty=".$this->searchProperty;
          else
              $this->searchStr = $this->searchStr."searchProperty=".$this->searchProperty;
      }
      if($this->searchText != null)
          $this->searchStr = $this->searchStr."&searchText=".$this->searchText;
      if($this->searchStr !="")
          $this->searchStr = "&".$this->searchStr;
      if($searchDelete == "false")
           $query = $query->andWhere("status_id != 1006");
        $query = $query->orderBy('publish_time desc,photo_group_id desc');

      $this->pages = new sfDoctrinePager('PhotoGroup',$this->recordCount);//$this->currentPage
      $this->pages->setQuery($query);  //$q   数据库查询语句
      $this->pages->setPage($this->currentPage);//
      $this->pages->init();
  }

    public function executeEdit(sfWebRequest $request)
    {
        $execSql=$request->getParameter("albumEdit");
        $this->results="";
        $this->focTrue="false";
        $this->recTrue="false";
        $this->topTrue="false";

        switch($execSql){
            case "1"://加载
                $content=$request->getParameter("content");
                $this->focusId="";
                if(!empty($content))
                {
                    $editPhotoGroup=@Doctrine_Core::getTable('PhotoGroup')->find($content);
                    if(!empty($editPhotoGroup)){
                        if($editPhotoGroup->getIsFocusB())
                            $this->focTrue=true;
                        if($editPhotoGroup->getIsRecommendB())
                            $this->recTrue=true;
                        if($editPhotoGroup->getIsOnTopB())
                            $this->topTrue=true;
                        $this->photoGroup=$editPhotoGroup;//"s";//@$editVideo->delete();
                        if($this->photoGroup->getIsFocusB())
                        {
                           $focuLine= @Doctrine_Query::create()->from("FocusContent f")
                                    ->Where('content_id='.$this->photoGroup->getPhotoGroupId())
                                    ->andWhere('content_type_i=1003')->execute();
                            foreach($focuLine as $foc)
                            {
                                $this->focusId=@$foc->getFocusContentId();
                                break;
                            }
                        }
                        $this->photos= @Doctrine_Query::create()->from("Photo p")
                                    ->Where('p.photo_group_id='.$this->photoGroup->getPhotoGroupId())
                                    ->orderBy('p.order_i asc')->execute();
                    }
                }
                break;
            case "2"://修改
                $content=$request->getParameter("content");
                if(!empty($content))
                {
                    $photoGroup=@Doctrine_Core::getTable('PhotoGroup')->find($content);
                    if(!empty($photoGroup))
                    {
                        $photoGroup->setCatalogId($request->getParameter("sCatalogId"));
                        $titles=$request->getParameter("title");
                        $photoGroup->setTitle($titles);
                        $photoGroup->setTag($request->getParameter("tag"));
                        $photoGroup->setKeyword($request->getParameter("keyword"));
                        $photoGroup->setSnapshot($request->getParameter("shotHid"));
                        $focus=$request->getParameter("ckFocusContent");
                        $recommend=$request->getParameter("ckRecommendContent");
                        $tops=$request->getParameter("ckTop");
                        if($focus)
                            $photoGroup->setIsFocusB(1);
                        else
                            $photoGroup->setIsFocusB(0);
                        if($recommend)
                            $photoGroup->setIsRecommendB(1);
                        else
                            $photoGroup->setIsRecommendB(0);
                        if($tops)
                            $photoGroup->setIsOnTopB(1);
                        else
                            $photoGroup->setIsOnTopB(0);
                        $photoGroup->setIsHeaderB(0);
                        $urls="";
                        $indexs=$request->getParameter("firstPhoto");
                        if(1==$indexs)
                            $urls=$request->getParameter("Txt1");
                        else
                            $urls=$request->getParameter("Txt0");
                        $photoGroup->setSnapshot($urls);
                        $photoGroup->setIsAuditB(0);//$request->getParameter("auditing")
                        $photoGroup->setModifyTime(date("Y-m-d H:i:s"));
                        $photoGroup->setPublishTime($request->getParameter("nowtime"));
                        $photoGroup->setHitMaskI($request->getParameter("hitMark"));
                        $photoGroup->setSource($request->getParameter("sources"));
                        $capletter=@utility::getfirstchar($titles);
                        $photoGroup->setCapitalLetter($capletter);
                        $photoGroup->save();
                        $groupId=$photoGroup->getPhotoGroupId();
                        if($groupId>=1000)
                        {
                            if($focus)
                            {
                                $focusContent=new FocusContent();
                                $focuLine= Doctrine_Query::create()->from("FocusContent f")
                                 ->Where('content_id='.$groupId)
                                 ->andWhere('content_type_i=1003')->execute();
                                foreach($focuLine as $foc)
                                {
                                    if($foc->getFocusContentId()>0)
                                        $focusContent=@Doctrine_Core::getTable('FocusContent')->find($foc->getFocusContentId());
                                    break;
                                }
                                $focusContent->setContentId($photoGroup->getPhotoGroupId());
                                $focusContent->setContentTypeI(1003);
                                $focusContent->setCatalogId($photoGroup->getCatalogId());
                                $focusContent->setModifyTime(date("Y-m-d H:i:s"));
                                $focusContent->setIsOnTopB($tops);
                                $focusContent->setSubTitle($request->getParameter("txtFocusTitle"));
                                $focusContent->setShortWord($request->getParameter("txtFocusBrief"));
                                $focusContent->setFocusPhoto($request->getParameter("txtFocusLocalFile"));
                                $focusContent->save();
                            }else
                            {
                                //删除焦点
                                $focuLine= Doctrine_Query::create()->from("FocusContent f")
                                        ->Where('content_id='.$groupId)
                                        ->andWhere('content_type_i=1003')->execute();
                                foreach($focuLine as $foc)
                                {
                                    if($foc->getFocusContentId()>0)
                                    {
                                        $focusContent=@Doctrine_Core::getTable('FocusContent')->find($foc->getFocusContentId());
                                        @$focusContent->delete();
                                    }
                                }
                            }
                            if($recommend)
                            {
                                $recomendContent=new RecommendContent();
                                $recomLine= Doctrine_Query::create()->from("RecommendContent r")
                                        ->Where('r.content_id='.$groupId)
                                        ->andWhere('r.content_type_i=1003')->execute();
                                foreach($recomLine as $recom)
                                {
                                    if($recom->getRecommendContentId()>0)
                                        $recomendContent=@Doctrine_Core::getTable('RecommendContent')->find($recom->getRecommendContentId());
                                    break;
                                }
                                $recomendContent->setCatalogId($photoGroup->getCatalogId());
                                $recomendContent->setContentId($photoGroup->getPhotoGroupId());
                                $recomendContent->setModifyTime(date("Y-m-d H:i:s"));
                                $recomendContent->setContentTypeI(1003);
                                $recomendContent->setIsOnTopB($tops);
                                $recomendContent->save();
                            }else
                            {
                                $recomLine= Doctrine_Query::create()->from("RecommendContent r")
                                        ->Where('r.content_id='.$groupId)
                                        ->andWhere('r.content_type_i=1003')->execute();
                                foreach($recomLine as $recom)
                                {
                                    if($recom->getRecommendContentId()>0)
                                    {
                                        $recomendContent=@Doctrine_Core::getTable('RecommendContent')->find($recom->getRecommendContentId());
                                        @$recomendContent->delete();
                                    }
                                }
                            }

                            $editphotos= @Doctrine_Query::create()->from("Photo p")
                                    ->Where('p.photo_group_id='.$groupId)
                                    ->orderBy('p.order_i asc')->execute();
                            $i=1;
                            foreach($editphotos as $photo){
                                if($i==1&&1==$indexs)
                                {
                                    $hids=$request->getParameter("Txt".$i);
                                    if(null==$hids||""==$hids)
                                    {
                                        @$photo->delete();
                                        continue;
                                    }
                                    $hids=$urls;
                                }
                                else
                                {
                                    $hids=$request->getParameter("Txt".$i);
                                    if(null==$hids||""==$hids)
                                    {
                                        $photo->delete();
                                        continue;
                                    }
                                }
                                $photo->setLink($hids);
                                $photo->setBrief($request->getParameter("Des".$i));
                                $photo->setPhotoGroupId($groupId);
                                $photo->setOrderI($i);
                                $photo->save();
                                $i++;
                            }
                            for($i;$i<=20;$i++){
                                $photo=new Photo();
                                if($i==1&&1==$indexs)
                                    $hids=$urls;
                                else
                                {
                                    $hids=$request->getParameter("Txt".$i);
                                    if(null==$hids||""==$hids)break;
                                }
                                $photo->setLink($hids);
                                $photo->setBrief($request->getParameter("Des".$i));
                                $photo->setPhotoGroupId($groupId);
                                $photo->setOrderI($i);
                                $photo->save();
                            }
                        }
                    }
                }
                $this->redirect("album/index?catalogId=".$photoGroup->getCatalogId());
                //$this->forward("album","index");
                break;
        }
    }

  public function executeAdd(sfWebRequest $request)
  {
      $this->execSql=$request->getParameter("albumEdit");
      $this->results="";
      switch($this->execSql){
          case "1":
              $photoGroup=new PhotoGroup();
              $photoGroup->setCatalogId($request->getParameter("sCatalogId"));
              $titles=$request->getParameter("title");
              $photoGroup->setTitle($titles);
              $photoGroup->setTag($request->getParameter("tag"));
              $photoGroup->setKeyword($request->getParameter("keyword"));

              $focus=$request->getParameter("ckFocusContent");
              $recommend=$request->getParameter("ckRecommendContent");
              $tops=$request->getParameter("ckTop");
              if($focus)
                  $photoGroup->setIsFocusB(1);
              else
                  $photoGroup->setIsFocusB(0);
              if($recommend)
                  $photoGroup->setIsRecommendB(1);
              else
                  $photoGroup->setIsRecommendB(0);
              if($tops)
                  $photoGroup->setIsOnTopB(1);
              else
                  $photoGroup->setIsOnTopB(0);
              $photoGroup->setIsHeaderB(0);
              $urls="";
              $indexs=$request->getParameter("firstPhoto");
              if(1==$indexs)
                 $urls=$request->getParameter("Txt1");
              else
                  $urls=$request->getParameter("Txt0");
              $photoGroup->setSnapshot($urls);
              $photoGroup->setIsAuditB(0);//$request->getParameter("auditing")
              $photoGroup->setCreateTime(date("Y-m-d H:i:s"));
              $photoGroup->setModifyTime(date("Y-m-d H:i:s"));
              $photoGroup->setPublishTime($request->getParameter("nowtime"));
              $photoGroup->setHitI(0);
              $photoGroup->setStatusId(1004);//$request->getParameter("check"));
              $photoGroup->setHitMaskI($request->getParameter("hitMark"));
              $photoGroup->setSource($request->getParameter("sources"));
              $capletter=@utility::getfirstchar($titles);
              $photoGroup->setCapitalLetter($capletter);
              $photoGroup->save();
              $groupId=$photoGroup->getPhotoGroupId();

              if($groupId>=1000)
              {
                  if($focus)
                  {
                      $focusContent=new FocusContent();
                      $focusContent->setContentId($photoGroup->getPhotoGroupId());
                      $focusContent->setContentTypeI(1003);
                      $focusContent->setCatalogId($photoGroup->getCatalogId());
                      $focusContent->setCreateTime(date("Y-m-d H:i:s"));
                      $focusContent->setModifyTime(date("Y-m-d H:i:s"));
                      $focusContent->setIsOnTopB($tops);
                      $focusContent->setSubTitle($request->getParameter("txtFocusTitle"));
                      $focusContent->setShortWord($request->getParameter("txtFocusBrief"));
                      $focusContent->setFocusPhoto($request->getParameter("txtFocusLocalFile"));
                      $focusContent->setStatusId($photoGroup->getStatusId());
                      $focusContent->save();
                  }
                  if($recommend)
                  {
                      $recomendContent=new RecommendContent();
                      $recomendContent->setCatalogId($photoGroup->getCatalogId());
                      $recomendContent->setContentId($photoGroup->getPhotoGroupId());
                      $recomendContent->setCreateTime(date("Y-m-d H:i:s"));
                      $recomendContent->setModifyTime(date("Y-m-d H:i:s"));
                      $recomendContent->setContentTypeI(1003);
                      $recomendContent->setIsOnTopB($tops);
                      $recomendContent->save();
                  }
                  
                  for($i=1;$i<=20;$i++){
                      $photo=new Photo();
                      if($i==1&&1==$indexs)
                         $hids=$urls;
                      else
                      {
                          $hids=$request->getParameter("Txt".$i);
                          if(null==$hids||""==$hids)break; 
                      }
                      $photo->setLink($hids);
                      $photo->setBrief($request->getParameter("Des".$i));
                      $photo->setPhotoGroupId($groupId);
                      $photo->setOrderI($i);
                      $photo->save();                      
                  }
              }
              $this->redirect("album/index?catalogId=".$photoGroup->getCatalogId());
              break;

      }
  }
    
  function modifyPhotoRecommend($currentPhoto,$actionType)
   {
       $recommend = Doctrine_Core::getTable("recommendContent")->createQuery()
               ->where("content_id=?",$currentPhoto->getPhotoGroupId())
               ->andWhere("content_type_i=1003")
              ->execute()->getFirst();
      if($actionType == "add")
      {
          if($recommend == null)
          {
              $recommend = new RecommendContent();
              $recommend->setContentId($currentPhoto->getPhotoGroupId());
              $recommend->setContentTypeI(1002);
              $recommend->setCreateTime(date("Y-m-d H:i:s"));
              $recommend->setModifyTime(date("Y-m-d H:i:s"));
              $recommend->setCatalogId($currentPhoto->getCatalogId());
              $recommend->save();
          }
      }
      else
      {
          if($recommend != null)
          {
              switch($actionType)
              {
                  case "top":
                      $recommend->setIsOnTopB("1");
                      $recommend->save();
                      break;
                  case "cancelTop":
                      $recommend->setIsOnTopB("0");
                      $recommend->save();
                      break;
                  case "delete":
                      $recommend->delete();
                      break;
              }
          }
      }
   }

  function modifyPhotoFocus($currentPhoto, $actionType)
  {
      $focus = Doctrine_Core::getTable("FocusContent")->createQuery()
              ->where("content_id=?",$currentPhoto->getPhotoGroupId())
              ->andWhere("content_type_i=1003")
              ->execute()->getFirst();
      if($focus != null)
      {
          switch($actionType)
          {
              case "top":
                  $focus->setIsOnTopB("1");
                  break;
              case "cancelTop":
                  $focus->setIsOnTopB("0");
                  break;
              case "delete":
                  $focus->setStatusId(1006);
                  break;
              case "publish":
              case "cancelPublish":
                 $focus->setStatusId($currentPhoto->getStatusId());
                 $focus->setCreateTime(date("Y-m-d H:i:s"));
                 break;
          }
          $focus->save();
      }

  }
}
